package com.fc.common.log;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.fc.common.util.DateUtil;
import com.fc.common.util.IpUtils;
import com.fc.common.util.JacksonUtil;

import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.spi.StackTraceElementProxy;

/** 
 * @ClassName: LoggingHolder 
 * @Description: TODO
 * @author wanghw 
 * @date 2016年5月25日 
 */
public class LoggingHolder {
	
	
	private Map<String,LoggingEntity> logMap = new ConcurrentHashMap<String,LoggingEntity>();

	private static LoggingHolder single = new LoggingHolder();
	
	public static LoggingHolder instance(){
		return single;
	}
	
	public void append(LoggingEvent event,LoggingCallBack call){
		String content = event.getFormattedMessage()!=null?event.getFormattedMessage():"null";
		String threadName = event.getThreadName();
		LoggingEntity entity;
		//开始标识，放入map
		if(LoggingConfig.START_TAG.equals(content)){
			entity = new LoggingEntity();
			entity.setEnv(LoggingConfig.env);
			entity.setRoot_path("");
			entity.setApp(LoggingConfig.app);
			entity.setModule("");
			entity.setController("");
			entity.setRequest_get_paras("");
			entity.setRequest_post_paras("");
			entity.setDateline(System.currentTimeMillis()/1000);
			entity.setServer_ip(IpUtils.getLocalIP());
			entity.setCreatedate(DateUtil.getDateFormat(DateUtil.YMD).format(new Date()));
			logMap.put(threadName, entity);
		//结束
		}else if(LoggingConfig.END_TAG.equals(content)){
			entity = logMap.remove(threadName);
			if(call!=null){
				call.log(JacksonUtil.writeValue(entity));
			}
		//设置operation
		}else if(content.startsWith(LoggingConfig.OPERATION_TAG)){
			entity = logMap.get(threadName);
			entity.setOperate(content.replace(LoggingConfig.OPERATION_TAG, ""));
		//设置用户请求URL
		}else if(content.startsWith(LoggingConfig.CLIENT_IP_TAG)){
			entity = logMap.get(threadName);
			entity.setIp(content.replace(LoggingConfig.CLIENT_IP_TAG, ""));
		//设置用户请求URL
		} else if(content.startsWith(LoggingConfig.REQUEST_URI_TAG)){
			entity = logMap.get(threadName);
			entity.setRequest_uri(content.replace(LoggingConfig.REQUEST_URI_TAG, ""));
		//设置用户请求参数
		}else if(content.startsWith(LoggingConfig.REQUEST_PARAMS_TAG)){
			entity = logMap.get(threadName);
			entity.setRequest_get_paras(content.replace(LoggingConfig.REQUEST_PARAMS_TAG, ""));
		//设置控制器
		}else if(content.startsWith(LoggingConfig.CONTROLLER_TAG)){
			entity = logMap.get(threadName);
			String controllerStr = content.replace(LoggingConfig.CONTROLLER_TAG, "");
			String[] controllers = controllerStr.split("\\.");
			if(controllers.length==2){
				entity.setModule(controllers[0]);
				entity.setController(controllers[1]);
			}
			
		//记录日志
		}else{
			entity = logMap.get(threadName);
			if(entity!=null){
				String info;
				if(content.startsWith(LoggingConfig.SQL_TAG)){
		    		info = content;
		    	}else{
		    		info = event.getLevel()+":"+content;
		    	}
				
				entity.addLogs(info);
				IThrowableProxy throwableProxy = event.getThrowableProxy();
				if(throwableProxy!=null){
					StackTraceElementProxy[] stackTraceElements =  throwableProxy.getStackTraceElementProxyArray();
					if(stackTraceElements!=null && stackTraceElements.length>0){
						for(StackTraceElementProxy element : stackTraceElements){
							if(element!=null){
								entity.addLogs(event.getLevel()+":"+element.toString());
							}
						}
					}
				}
			}
		}
	}
	
}
